<?php
namespace Assist\Controller;

class ProblemController extends AssistController {
    public function index(){
        $category = $this->catalog();
        /* 获取当前分类列表 */
        $Document = D('Document');
        $list = $Document->page($p, $category['list_row'])->lists($category['id'],'`level` DESC,`id` DESC');
        if(false === $list){
            $this->error('获取列表数据失败！');
        }
        /* 模板赋值并渲染模板 */
        $this->assign('category', $category);
        $this->assign('list', $list);
        $this->display();
    }
    
    protected function catalog(){
        $category = D('Category')->info('Problem');
        if($category && 1 == $category['status']){
            switch ($category['display']) {
                case 0:
                    $this->error('该分类禁止显示！');
                    break;
                //TODO: 更多分类显示状态判断
                default:
                    return $category;
            }
        } else {
            $this->error('分类不存在或被禁用！');
        }
    }
    
    public function detail($id = 0, $p = 1){
        /* 标识正确性检测 */
        if(!($id && is_numeric($id))){
            $this->error('文档ID错误！');
        }

        /* 页码检测 */
        $p = intval($p);
        $p = empty($p) ? 1 : $p;

        /* 获取详细信息 */
        $Document = D('Document');
        $info = $Document->detail($id);
        if(!$info){
            $this->error($Document->getError());
        }

        /* 更新浏览数 */
        $map = array('id' => $id);
        $Document->where($map)->setInc('view');

        /* 模板赋值并渲染模板 */
        $this->assign('category', $category);
        $this->assign('info', $info);
        $this->assign('page', $p); //页码
        $this->display();
    }
}